package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.utils.JwtUtils;
import com.itheima.common.utils.Md5Util;
import com.itheima.wemedia.pojo.WmUser;
import com.itheima.wemedia.mapper.WmUserMapper;
import com.itheima.wemedia.service.WmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author czh
 * @since 2021-08-20
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    @Autowired
    private WmUserMapper wmUserMapper;

    /**
     * 登录
     * @param wmUser
     * @return
     */
    @Override
    public String login(WmUser wmUser) {
        //得到用户名
        String name = wmUser.getName();
        //根据用户名查询是否有这个用户
        QueryWrapper<WmUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        WmUser user = wmUserMapper.selectOne(queryWrapper);
        if (user != null) {
            //得到盐
            String salt = user.getSalt();
            //得到登录密码的加密
            String pwd = Md5Util.toMD5(wmUser.getPassword(), salt);
            //判断两个密码是否一致
            if (pwd.equals(user.getPassword())){
                //将密码设置一下，创建token
                user.setPassword("*");
                Map<String,Object> dataMap = BeanMap.create(user);
                return JwtUtils.token(dataMap);

            }

        }
        return null;
    }
}
